package com.zhx.taoda;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.*;
import com.zhx.taoda.model.TemplateField;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

public class ConvertService {
    private static final Logger log = Logger.getLogger("ConvertService");

    /**
     * 构建模板字段
     *
     * @param data
     * @return
     */
    public static List<TemplateField> buildTemplateField(String data) {
        List<TemplateField> list = new ArrayList<>();
        String[] itemArrays = data.split("\n");
        for (String item : itemArrays) {
            list.add(new TemplateField(item));
        }
        return list;
    }

    /**
     * 构建模板字段
     *
     * @param excelList @return
     */
    public static List<TemplateField> buildTemplateField(List<List<String>> excelList) {
        List<TemplateField> list = new ArrayList<>();
        for (int i = 1; i < excelList.size(); i++) {
            List<String> temp = excelList.get(i);
            list.add(new TemplateField(temp));
        }
        return list;
    }

    public static void convertPdf(String templatePdf, String outputPdf, Map<String, Object> data) {
        PdfStamper ps = null;
        PdfReader reader = null;
        OutputStream os = null;
        try {
            os = new FileOutputStream(outputPdf);
            reader = new PdfReader(templatePdf);
            ps = new PdfStamper(reader, os);
            AcroFields form = ps.getAcroFields();
            BaseFont bf = BaseFont.createFont("./simhei.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            form.addSubstitutionFont(bf);
            for (String key : data.keySet()) {
                form.setField(key, data.get(key).toString());
            }
            ps.setFormFlattening(true);
            log.info("convert [" + templatePdf + " -> " + outputPdf + "] pdf success.");
        } catch (Exception e) {
            log.info("convert [" + templatePdf + " -> " + outputPdf + "] pdf error: " + e.getMessage());
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }
                if (reader != null) {
                    reader.close();
                }
            } catch (Exception e) {
                log.info("close resource " + e.getMessage());
            }
        }
    }

    /**
     * 创建空pdf文件
     * @param pdfPath
     */
    public static void createEmptyPdf(String pdfPath) {
        Document document = new Document();
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
            document.open();
            document.add(new Paragraph("empty."));
            document.close();
            writer.close();
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
